package Util::Users::UsersSearch;
use Moose;
use namespace::autoclean;
use JSON;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);



sub get_user_by_nick {
        my ($self, $source_id, $nick) = @_;
        return $self->schema->resultset('User')->search(
                {
                        'source_nick.source_id' => $source_id,
                        'source_nick.source_nick' =>  $nick},
                {
                        join=> 'source_nick'
#                       columns => [qw/active source_nick.source_id source_nick.source_nick/]
                });
}

sub get_tuanzhang_for_university {
        my ($self, $university_id) = @_;
        my @tuanzhangs = $self->schema->resultset('User')->search(
                {'user_tuan.role' => 'C',
		'me.university_id' => "$university_id",
                 'user_tuan_2.role' => { -in => ['J','F','Q'] }},
                {
                        '+select' => [
                                {count => 'user_tuan_2.user_id', -as=> 'user_count'}
                        ],
                        group_by => [qw/me.id/],
			rows=>5,
                        join => {'user_tuan' => 'user_tuan'},
                        order_by => {-desc => [qw/user_count/]}
                }
        );
	return \@tuanzhangs;
}



sub renren_get_user_by_nick {
	my ($self, $source_id, $nick) = @_;
	my @users = $self->schema->resultset('User')->search(
		{
			'source_nick.source_id' => $source_id,
			'source_nick.source_nick' =>  $nick},
		{	
			select => [
				'me.id',
				'me.username',
				'source_nick.source_nick',
				'source_nick.source_id'
			],
			as => [qw/
				id
				username
				source_nick
				source
			/],
			join=> 'source_nick'
		});
	return \@users;
}
#####################################################
#Input: 
#	user_name -- username to search
#	page -- page number of content to return 
#Return: 
#	@users -- A list of users meet the search criteria
#####################################################
sub user_search {
	my ($self, $user_name, $page) = @_;
	$user_name =~ s/^\s+// if defined($user_name);
	$user_name =~ s/\s+$// if defined($user_name);

	if(defined($user_name)) {
		$user_name = "%" . $user_name . "%";
	} else {
		$user_name = "%%";
	}

	my $rows = 20;
	unless(defined($page)) {
		if ($user_name eq '%%' ) {
			return $self->schema->resultset('User')->search(
				{},
				{ order_by => {-desc=>"created"}});
		} else {
			return $self->schema->resultset('User')->search(
				{'me.username' => { like => $user_name}},
				{ order_by => {-desc=>"created"}},
			);
		} 
	} else {
		if ($user_name eq '%%' ) {
			return $self->schema->resultset('User')->search(
				{},
				{page=>$page,
				rows => $rows,
				order_by => {-desc=>"created"}},
			);
		} else {
			return $self->schema->resultset('User')->search(
				{'me.username' => { like => $user_name}},
				{page=>$page,
				rows => $rows,
				order_by => {-desc=>"created"}},
			);
		} 

	}

}

#####################################################
#Input: 
#       user_name -- username to search
#Return: 
#       result -- User names meet the search criteria in JSON format 
#####################################################
sub get_usernames_in_json {
	my ($self, $user_name) = @_;
	my @users;
	$user_name =~ s/^\s+// if defined($user_name);
	$user_name =~ s/\s+$// if defined($user_name);

	if(defined($user_name)) {
		$user_name = "%" . $user_name . "%";
	} else {
		$user_name = "%%";
	}

	if ($user_name eq '%%' ) {
		@users = $self->schema->resultset('User')->search();
	} else {
		@users = $self->schema->resultset('User')->search(
				{'me.username' => { like => $user_name}},
				{},
				);
	} 
        my %result;
        my $i = 0;
        foreach (@users){
                my $value = $_->username;
                $value =~ s/^\s+//;
                $value =~ s/\s+$//;
                $result{$value} = 1 unless (exists $result{$value});
        }
        my @keys = keys %result;
        my $json_text = to_json(\@keys);
}

#####################################################
#Input: 
#       user_id -- user_id to search
#Return: 
#       Users meet the search criteria
#####################################################
sub find{
	my ($self, $user_id) = @_;
	return $self->schema->resultset('User')->find($user_id);
}

#####################################################
#Input: 
#       user_name -- user_name to check 
#Return: 
#      true -- does not exist 
#	false -- already been registered
#####################################################
sub username_check{
	my ($self, $user_name) = @_;
	my $count = $self->schema->resultset('User')->search({username => $user_name}, {})->count;
	if ($count == 0){
                return 'true';
        } else {
                return 'false';
        }

}

#####################################################
#Input: 
#       email
#Return: 
#     	true -- does not exist 
#	false -- already been registered
#####################################################
sub email_check{
	my ($self, $email) = @_;
	my $count = $self->schema->resultset('User')->search({email_address=> $email}, {})->count;
	if ($count == 0){
                return 'true';
        } else {
                return 'false';
        }

}

sub email_check_2{
	my ($self, $email) = @_;
	my @user = $self->schema->resultset('User')->search({email_address=> $email}, {});
	if (defined $user[0]){
		if($user[0]->active eq 'Y') {
			return 'true';
		} else {
			return 'false';
		}
        } else {
                return 'false';
        }

}
sub get_user_by_email{
	my ($self, $email) = @_;
	$email =~ s/^\s+//;
	$email =~ s/\s+$//;

	return $self->schema->resultset('User')->search({email_address=> $email}, {});
}

sub get_user_by_username{
	my ($self, $username) = @_;
	$username =~ s/^\s+//;
	$username =~ s/\s+$//;

	return $self->schema->resultset('User')->search({username => $username}, {});
}

sub get_random_users_by_university {
	my ($self, $university_id) = @_;

	my @users = $self->schema->resultset('User')->search({university_id => $university_id}, {order_by => {-desc => [qw/created/]},rows=>20});
	return \@users;
}



__PACKAGE__->meta->make_immutable;

1;
